from django.contrib import admin
from .models import User
from .models import UserMobile
from .models import UserQq
from .models import UserWechat
from order.models import Order


class UserMobileTabularInline(admin.TabularInline):
	model = UserMobile
	readonly_fields = ('mobile',)
	can_delete = False

class UserQqTabularInline(admin.TabularInline):
	model = UserQq
	readonly_fields = ('union_id',)
	can_delete = False

class UserWechatTabularInline(admin.TabularInline):
	model = UserWechat
	readonly_fields = ('union_id',)
	can_delete = False

class OrderTabularInline(admin.TabularInline):
	model = Order
	list_display = ('id','code','user_account_id','pay_type_id','amount',)
	readonly_fields = ('gmt_create','gmt_modified','pay_type')
	exclude = ['code','pay_type_id']
	can_delete = False

class UserAdmin(admin.ModelAdmin):

	class Meta:
		model = User
	# cautious！！ do not user phone, it will cause material jquery hidding the cloumn
	def phone(self, obj):
		return obj.usermobile.mobile
	phone.short_description = '手机'

	def qq(self, obj):
		if obj.userqq.union_id:
			return True
		else:
			return None

	def wechat(self,obj):
		if obj.userwechat.union_id:
			return True
		else:
			return None

	list_per_page = 20
	icon = '<i class="material-icons">account_box</i>'
	readonly_fields = ('id','salt','password')
	list_display = ('id','phone','qq','wechat')
	list_filter = ('is_actived', 'gender',)
	search_fields = ('user_name', 'id', 'usermobile__mobile')
	readonly_fields = ('gmt_create','gmt_modified','delete_time','is_deleted','passivable_time','last_login_time')
	inlines = [UserMobileTabularInline,UserQqTabularInline,UserWechatTabularInline,OrderTabularInline]


admin.site.register(User,UserAdmin)
